package com.example.droidesk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String usuario = "hola";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				Toast toastlogin = Toast.makeText(getApplicationContext(),getRequest(), Toast.LENGTH_SHORT);
				toastlogin.show();
				Intent intent = new Intent(MainActivity.this, principal.class);
				Bundle bundle = new Bundle();
				bundle.putString("NOMBRE", usuario);
				intent.putExtras(bundle);
				startActivity(intent);
			}
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public String getRequest() {
        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        Log.v("morir", "morir");
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://dominiodigital.zendesk.com/users.xml");
            httpGet.setURI(uri);
            Log.v("morir", "morir antes de crear");
            
            EditText user = (EditText)findViewById(R.id.editText1);
        	EditText pass = (EditText)findViewById(R.id.editText2);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials(user.getText().toString(), pass.getText().toString()),
                    HTTP.UTF_8, false));

            HttpResponse httpResponse = httpClient.execute(httpGet);
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));

            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                stringBuffer.append("\n");
                readLine = bufferedReader.readLine();
            }
            //Log.v("info",stringBuffer.toString());
            usuario = user.getText().toString();
        } catch (Exception e) {
            // TODO: handle exception
        	Log.v("morir", e.toString());
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }
        }
        return stringBuffer.toString();
    }
    
}
